var appdb;

function initializeDB(name)
{
try {
if (!window.openDatabase) {
alert('not supported');
} else {
var shortName = 'mydatabase';
var version = '1.0';
var displayName = 'My Important Database';
var maxSize = 65536; // in bytes
var mydb = openDatabase(shortName, version, displayName, maxSize);
// You should have a database instance in mydb.
}
} catch(e) {
// Error handling code goes here.
if (e == INVALID_STATE_ERR) {
// Version number mismatch.
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}
return;
}
// alert("Database is: "+mydb);
appdb=mydb;

createPeoplesTable(name);
}


function deleteEventTable()
{
	initializemDB();
	 var db=appdb;
	  db.transaction(
	  function (transaction) {
		  transaction.executeSql('DROP TABLE event;',[], nullDataHandler, errorHandler);
	}
	);
	  //alert("table deleted");
}

function createPeoplesTable(name)
{
  var db=appdb;
  db.transaction(
  function (transaction) {
  transaction.executeSql('CREATE TABLE IF NOT EXISTS event(id INTEGER NOT NULL	PRIMARY KEY AUTOINCREMENT, eventname TEXT NOT NULL );', [], nullDataHandler, errorHandler);
}
);
  var events = new Array();
  events = name.split(',');
  for (var i=0; i<events.length; i++)
  {
insertPeoplesTable(events[i]);
  }
}

function insertPeoplesTable(name)
{
var db=appdb;
//alert(name);
	var sql='insert into event (eventname) VALUES ('+'"'+name+'");';
	db.transaction(
function (transaction) {
transaction.executeSql(sql, [], nullDataHandler, errorHandler);
}
);

	updateEventList();
}

function updateEventList()
{
	var db=appdb;
	  db.transaction(
	  function (transaction) {
	     transaction.executeSql("SELECT * from event ;",[], dataHandler, errorHandler1);
	    }
	  );
}

function getEventsFromDb()
{
	initializemDB();
	
//	alert("getting events from db");
 var db=appdb;
  db.transaction(
  function (transaction) {
     transaction.executeSql("SELECT * from event ;",[], dataHandler, errorHandler1);
    }
  );
}
function errorHandler1(transaction, error)
{
    //  Error is a human-readable string.
     alert('OopsDops.  Error was '+error.message+' (Code '+error.code+')');
    //deleteEventTable();
    // Handle errors here
    var we_think_this_error_is_fatal = true;
    if (we_think_this_error_is_fatal) return true;
    return false;
}

function errorHandler(transaction, error)
{
    //  Error is a human-readable string.
     alert('Oops.  Error was '+error.message+' (Code '+error.code+')');
    //deleteEventTable();
    // Handle errors here
    var we_think_this_error_is_fatal = true;
    if (we_think_this_error_is_fatal) return true;
    return false;
}

function initializemDB()
{
try {
if (!window.openDatabase) {
// alert('not supported');
} else {
var shortName = 'mydatabase';
var version = '1.0';
var displayName = 'My Important Database';
var maxSize = 65536; // in bytes
var mydb = openDatabase(shortName, version, displayName, maxSize);
// You should have a database instance in mydb.
}
} catch(e) {
// Error handling code goes here.
if (e == INVALID_STATE_ERR) {
// Version number mismatch.
// alert("Invalid database version.");
} else {
// alert("Unknown error "+e+".");
}
return;
}
// alert("Database is: "+mydb);
appdb=mydb;
}


function nullDataHandler(transaction, results)
{
}


function dataHandler(transaction, results)
{
	
	var Parent = document.getElementById('eventList');
    var string = "";
    
    if ( Parent.hasChildNodes() )
    {
        while ( Parent.childNodes.length >= 1 )
        {
        	Parent.removeChild( Parent.firstChild );       
        } 
    }

    var planetsLI = document.createElement("LI");
	planetsLI.innerHTML ='<a href="#" onclick="planetaryWatch();">Planets</a>';
	Parent.appendChild(planetsLI);
	
   for (var i=0; i<results.rows.length; i++) {
       var row = results.rows.item(i);
   	var NewLI = document.createElement("LI");
       //alert("row is "+row);
      // NewLI.innerHTML ='<a href="/UserEventInstance.jsp?eventName='+row['eventname']+'">'+row['eventname']+'</a>';
       //string = string + row['eventname'] + '<br>';
     // 	NewLI.innerHTML ='<a href="#" onclick="showEventForm('+row['eventname']+')">'+row['eventname']+'</a>';
   	NewLI.innerHTML ='<a href="#" onclick="showEventForm(\''+row['eventname']+'\');return false;">'+row['eventname']+'</a>';
       Parent.appendChild(NewLI);
  	
     }
     //alert("in datahandler");
	
	   
//document.getElementById('toolbar').innerHTML=string;
}

function showEventForm(name)
{
	if (name!='cancel')
	{
	document.getElementById('eventListdiv').style.display='none';
	document.getElementById('eventList').style.display='none';
	document.getElementById('eventForm').style.display='block';
	document.getElementById('searchForm').style.display='block';
	showEventDate();
	document.getElementById('eventForm').innerHTML='<h1 id="pageTitle">'+name+'</h1>';
	document.getElementById('eventName').value=name;
	
	}
	else
	{
		document.getElementById('eventListdiv').style.display='block';
		document.getElementById('eventList').style.display='block';
		document.getElementById('eventForm').style.display='none';
		document.getElementById('searchForm').style.display='none';
		
	}
}




function showEventDate()
{
	var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
	var hours = currentTime.getHours() 
	if (hours < 10){
     hours = "0" + hours
     } 
	
    var minutes = currentTime.getMinutes()
	if (minutes < 10){
     minutes = "0" + minutes
     }

    var secs=currentTime.getSeconds()  
    if (secs < 10){
     secs = "0" + secs
     } 
	document.getElementById("eventTimestamp").value=month + "/" + day + "/" + year+" "+hours+":"+minutes+":"+secs
}





